package com.jane.adapter.hostheadlinesadapter;

import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jane.bean.OfflineBean;
import com.jane.gzzb.PlatformDetailActivity;
import com.jane.gzzb.R;
import com.jane.utils.Utils;

import java.util.ArrayList;

/**
 * Created by Grace on 2016/11/15.
 */

public class PlatformAdapter extends BaseAdapter {
    private Context mContect;
    private ArrayList<OfflineBean> mData;

    public PlatformAdapter(Context mContect, ArrayList<OfflineBean> mData) {
        this.mContect = mContect;
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData==null?0:mData.size();
    }

    @Override
    public Object getItem(int i) {
        return getItem(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view==null){
            view=View.inflate(mContect,R.layout.platform_activity_item,null);
            holder=new ViewHolder(view);
            view.setTag(holder);
        }else {
            holder= (ViewHolder) view.getTag();
        }
        Utils.loadImage(holder.mImageView,mData.get(i).getIconurl());
        holder.mTextViewTitle.setText(mData.get(i).getDisplayname());
        holder.mTextViewDetail.setText(mData.get(i).getDescription());
        //获取下载路径
        final String loadUrl=mData.get(i).getAndroiddownloadurl();
        holder.mButton.setOnClickListener(new View.OnClickListener        () {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri uri=Uri.parse(loadUrl);
                intent.setData(uri);
                mContect.startActivity(intent);

            }
        });
        final String name=mData.get(i).getName();
        final String disPlayname=mData.get(i).getDisplayname();
        holder.mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContect,PlatformDetailActivity.class);
                intent.putExtra("loadUrl",loadUrl);
                intent.putExtra("btnTitle",name);
                intent.putExtra("title",disPlayname);
                mContect.startActivity(intent);
            }
        });
        return view;
    }
    public static class ViewHolder{
        private ImageView mImageView;
        private Button mButton;
        private TextView mTextViewTitle,mTextViewDetail;
        public ViewHolder (View itemView){
            mImageView= (ImageView) itemView.findViewById(R.id.iv_item);
            mTextViewTitle= (TextView) itemView.findViewById(R.id.tv_title_item);
            mTextViewDetail= (TextView) itemView.findViewById(R.id.tv_detail_item);
            mButton= (Button) itemView.findViewById(R.id.btn_load);
        }
    }
}
